package CtrlLayer;

import java.util.ArrayList;
import ModelLayer.*;

/**
 * Write a description of class SaleController here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaleController
{
    // instance variables - replace the example below with your own
    private SaleContainer saleContainer;
    private ArrayList<SalesLine> saleLine;
    private Employee employee;
    private Customer customer;
    private ProductContainer productContainer;
    private CustomerContainer customerContainer;
    /**
     * Constructor for objects of class SaleController
     */
    public SaleController()
    {
        saleContainer = SaleContainer.getInstance();
        saleLine = new ArrayList<SalesLine>();
        productContainer = ProductContainer.getInstance();
    }
    
    
    public boolean addSale(Sale sale)
    {
        return saleContainer.addSale(sale);
    }
    
    public void startSale(int idC, int idE)
    {
        this.employee = EmployeeContainer.getInstance().findEmployeeById(idE);
        this.customer = CustomerContainer.getInstance().findCustomerById(idC);
        saleLine = new ArrayList<>();
    }
    

    public void addSaleLine(int barcode, int quantity)
    {
        SalesLine saleLine1 = new SalesLine(productContainer.findProductByBarCode(barcode), quantity);
        saleLine.add(saleLine1);
    }
    
    public double finishSale()
    {
        double totalPrice = 0;
        double totalDiscountPrice = 0;
        for(SalesLine tmp : saleLine)
        {
            totalPrice = totalPrice + (tmp.getProduct().getPrice() * tmp.getSalesQuantity());
        }
        //         for (SalesLine tmp : saleLine)
        //         {
        //             totalDiscountPrice = totalDiscountPrice + (tmp.getSalesQuantity() * 100 / 100* tmp.getProduct().getPrice());
        //         }
        int totalDiscount = 0;
        if(totalDiscount > 25)
        {
            totalDiscount = 25;
        }
        
        
        return totalPrice;
        
    }
    
    public void updateSale (int saleId, int barcode, int quantity)
    {
        findSaleById(saleId).findProductInListByBarCode(barcode).setQuantity(quantity);
    }
    
    public Sale findSaleById(int saleId)
    {
        return saleContainer.findSaleById(saleId);
    }
    
    public boolean deleteSaleById(int saleId)
    {
        return saleContainer.deleteSaleById(saleId);
    }
    
    public double getTotalPrice(int saleId)
    {
        return saleContainer.findSaleById(saleId).getTotalPrice();
    }
}
